package com.lianchuang.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
	public static final String DATE_FORMAT_LONG = "yyyy-MM-dd HH:mm:ss";
	public static String formatDateToString(String zoneId, Date date,
			String format) {
		String result = null;
		if (date == null) {
			result = "";
		} else {
			SimpleDateFormat stdFormat = new SimpleDateFormat(format);
			if (zoneId != null && !"".equals(zoneId)) {
				stdFormat.setTimeZone(TimeZone.getTimeZone(zoneId));
			}
			result = stdFormat.format(date);
		}
		return result;
	}
	public static String formatDateToString(Date date, String format) {
		return formatDateToString(null, date, format);
	}
	public static Date stringToDate(String dateString,String format) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);  
	    Date date = sdf.parse(dateString);  
	    return date;
	}
	public static String dateToString(Date date,String forMat){
		DateFormat dataFormat = new SimpleDateFormat(forMat);
		return dataFormat.format(date);
	}
	public static String formatDateToString(Date date) {
		return formatDateToString(date, DATE_FORMAT_LONG);
	}
	
	/****
	 * 字符串转换成Date类型
	 * 
	 * @param strDate
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date strToDate(String strDate, String format) throws ParseException {
		DateFormat df = new SimpleDateFormat(format);
		return df.parse(strDate);
	}
	
	/**
	 * 日期加上月数的时间
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date dateAddMonth(Date date,int month){
		return add(date,Calendar.MONTH,month);
	}
	
	/**
	 * 日期加上天数的时间
	 * @param date
	 * @param month
	 * @return
	 */
	public static Date dateAddDay(Date date,int day){
		return add(date,Calendar.DAY_OF_YEAR,day);
	}
	
	/**
	 * 日期加上年数的时间
	 * @param date
	 * @param year
	 * @return
	 */
	public static Date dateAddYear(Date date,int year){
		return add(date,Calendar.YEAR,year);
	}
	public static Date dateAddhour(Date date,int hour){
		return add(date,Calendar.HOUR_OF_DAY,hour);
	}
	
	 /****
     * 根据类型添加日期
     * @param date
     * @param type 1-年,2-月，5-天
     * @param value
     * @return
     */
	public static Date add(Date date,int type,int value){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(type, value);
		return calendar.getTime();
	}
	
	public static int getYear(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}
	
	
	public static int compareDate(Date date1,Date date2,int stype){ 
		date2 = date2==null?new Date():date2; 
		return compareDate(formatDate(date1),formatDate(date2),stype);
	}

	public static String formatDate(Date paramDate) {
		if (paramDate == null)
			return null;
		SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		localSimpleDateFormat.setLenient(false);
		return localSimpleDateFormat.format(paramDate);
	}
	
	 /**  
     * @param date1 需要比较的时间 不能为空(null),需要正确的日期格式  
     * @param date2 被比较的时间  为空(null)则为当前时间  
     * @param stype 返回值类型   0为多少天，1为多少个月，2为多少年  
     * @return  
     */ 
    public static int compareDate(String date1,String date2,int stype){  
        int n = 0;  
          
        String[] u = {"天","月","年"};  
        String formatStyle = stype==1?"yyyy-MM":"yyyy-MM-dd";  
          
        date2 = date2==null?getCurrentDate():date2;  
          
        DateFormat df = new SimpleDateFormat(formatStyle);  
        Calendar c1 = Calendar.getInstance();  
        Calendar c2 = Calendar.getInstance();  
        try {  
            c1.setTime(df.parse(date1));  
            c2.setTime(df.parse(date2));  
        } catch (Exception e3) {  
            System.out.println("wrong occured");  
        }  
        //List list = new ArrayList();  
        while (!c1.after(c2)) {                     // 循环对比，直到相等，n 就是所要的结果  
            //list.add(df.format(c1.getTime()));    // 这里可以把间隔的日期存到数组中 打印出来  
            n++;  
            if(stype==1){  
                c1.add(Calendar.MONTH, 1);          // 比较月份，月份+1  
            }  
            else{  
                c1.add(Calendar.DATE, 1);           // 比较天数，日期+1  
            }  
        }  
          
        n = n-1;  
          
        if(stype==2){  
            n = (int)n/365;  
        }     
        //System.out.println(date1+" -- "+date2+" 相差多少"+u[stype]+":"+n);        
        return n;  
    }  
      
    /**  
     * 得到当前日期  
     * @return  
     */ 
    public static String getCurrentDate() {  
        Calendar c = Calendar.getInstance();  
        Date date = c.getTime();  
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");  
        return simple.format(date);  
    }  
    
    /**  
     * 得到当前日期第一天
     * @return  
     */ 
    public static String getFirstDate(Date date) {  
    	 SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        //获取前月的第一天
        Calendar   cal_1=Calendar.getInstance();//获取当前日期 
        cal_1.setTime(date);
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        return  simple.format(cal_1.getTime());
    }  
    public static Date getMonthFirstDay(Date date){
    	Calendar cal = Calendar.getInstance();
    	cal.setTime(date);
    	cal.set(Calendar.DAY_OF_MONTH, 1);
    	return cal.getTime();
    }
    /**  
     * 得到当前日期最后一天  
     * @return  
     */ 
    public static String getLastDate(Date date) {
    	SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();   
        cale.setTime(date);
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
        return  simple.format(cale.getTime());
    }  

	public static String getFewDayAndMonth(Date date1,Date date2){
		if(date1.getTime()>date2.getTime()){
			return "0天";
		}
		long subMonth = DateUtil.getMonth(date1, date2);
		long subDays = DateUtil.diffDays(DateUtil.dateAddMonth(date1, (int) subMonth), date2);
		if(subMonth==0&&subDays==0){
			return subDays+"天";
		}
		if(subMonth==0&&subDays!=0){
			return subDays+"天";
		}
		if(subMonth!=0&&subDays==0){
			return subMonth+"个月";
		}
		return subMonth+"个月"+subDays+"天";
		
	}
    
	public static long getMonth(Date startDate, Date endDate) {
		long monthday = 0;

		Calendar starCal = Calendar.getInstance();
		starCal.setTime(startDate);

		int sYear = starCal.get(Calendar.YEAR);
		int sMonth = starCal.get(Calendar.MONTH);
		int sDay = starCal.get(Calendar.DATE);

		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);
		int eYear = endCal.get(Calendar.YEAR);
		int eMonth = endCal.get(Calendar.MONTH);
		int eDay = endCal.get(Calendar.DATE);

		monthday = ((eYear - sYear) * 12 + (eMonth - sMonth));

		if (sDay > eDay) {
			monthday = monthday - 1;
		}
		return monthday;
	}
	
	public static Date removeTime(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	public static long diffDays(Date startDate, Date endDate) {
		startDate = removeTime(startDate);
		endDate = removeTime(endDate);
		long days = 0;
		long start = startDate.getTime();
		long end = endDate.getTime();
		// 一天的毫秒数1000 * 60 * 60 * 24=86400000
		days = (end - start) / 86400000;
		return days;
		/*Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		int day1 = calendar.get(Calendar.DAY_OF_YEAR);
		int year1 = calendar.get(Calendar.YEAR);
		calendar.setTime(endDate);
		int day2 = calendar.get(Calendar.DAY_OF_YEAR);
		int year2 = calendar.get(Calendar.YEAR);
		return (year2-year1)*365+day2-day1;*/
	}
}
